package system;

import iddss.ClientWorker;
import iddss.Parameters;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import plugin.PluginLoader;


/**
 * Read settings from settings.ini
 * @author zem
 */
public class ReadSettings {

    /**
     * Read settings from settings.ini
     */
    public ReadSettings() {
        BufferedReader in;
        try {
//////open file for read
            FileInputStream fis = new FileInputStream("settings.ini");
            BufferedReader myInput = new BufferedReader(new InputStreamReader(fis));
            String thisLine = "";

//loop until EOF
//reading lines to an array

            while ((thisLine = myInput.readLine()) != null) {
                if (thisLine.length() > 2) {
                    char[] tmp = thisLine.toCharArray();
                    if (tmp[0] != '#') { //if line is not comment
                        int i = 0;
                        String des = ""; //desctription
                        while (tmp[i] != '=') { //while not end of description
                            des += tmp[i];
                            i++;
                        }
                        i++;
                        i++;//skipping char: "
                        String value = "";
                        while (tmp[i] != '"') {
                            value += tmp[i];
                            i++;
                        }



                        if (des.equals("autoload_plugins")) {
                            Parameters.AutostartPluginsList = value.split("&"); //autoloaded plugins
                        }
                        if (des.equals("key_folder")) {
                            Parameters.KeyFolder=value; //folder where user keys are saved
                        }
                        if (des.equals("port")){
                            Parameters.port=Integer.parseInt(value); //local port
                        }
                        if (des.equals("max_connections")){
                            Parameters.connection=new ClientWorker[Integer.parseInt(value)]; //connections
                            Parameters.connection_is_used=new boolean[Integer.parseInt(value)]; // true if connection in use
                        }
                        if (des.equals("key_generation_period")){
                            Parameters.KeyGenerationPeriod=Integer.parseInt(value);
                        }
                        if (des.equals("plugin_folder")){
                            Parameters.PluginDirectory=value;
                        }
                        if (des.equals("max_number_of_plugins")) { //maximum number of working plugins.
                            Parameters.LoadedPlugins=new PluginLoader[Integer.parseInt(value)];
                            Parameters.LoadedPluginNames=new String[Integer.parseInt(value)];
                        }
                    }
                }
            }

            myInput.close();
            fis.close();
        } catch (Exception error) {
            System.err.println(error);
        }
    }
}
